package dummy;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.Locale;

public strictfp class FloatTest {
	public static String to32bitstring(int x) {
		int n = 32;
		char[] res = new char[n];
		for (int i=0; i<n; i++) {
			res[n-i-1] = (char)('0' + (x & 1));
			x >>>= 1;
		}
		return new String(res);
	}
	
	public static String splitIEEE754(String bits) {
		String separator = " ";
		switch (bits.length()) {
		case 32:
			return bits.charAt(0)
					+ separator
					+ bits.substring(1, 9)
					+ separator
					+ bits.substring(9);			
		case 64:
			throw new UnsupportedOperationException("64 bits unsupported");
		default:
			throw new IllegalArgumentException("String must contain 32 or 64 bits");
		}
	}
	
	public static String fpToString(float f) {
		return splitIEEE754(to32bitstring(Float.floatToIntBits(f)));
	}
	
    public static void main( String[] args )
    {	
    	//test different rounding policies
    	Locale.setDefault(Locale.US);
    	System.out.println((int)(-1.6));
    	System.out.println((int)(-1.5));
    	System.out.println(Math.floor(-1.5));
    	System.out.println(Math.ceil(-1.5));
    	System.out.println(Math.rint(-1.5));    	
    	System.out.println(Math.round(-1.5));
    	

    	System.out.println(Math.rint(-2.5));    	
    	System.out.println(Math.round(-2.5));
    	
    	
    	//test decimal representation
    	BigDecimal x1 = new BigDecimal(10101, new MathContext(3, RoundingMode.UP));    	
    	System.out.println(x1);
    	
    	//test formatting
    	System.out.format("%.10f", 0.1);
    	
    	//researching IEEE754
    	System.out.println(fpToString(1));
    	System.out.println(fpToString(-1));
    	System.out.println(fpToString(Float.NaN));
    	System.out.println(fpToString(Float.NEGATIVE_INFINITY));
    	System.out.println(fpToString(Float.POSITIVE_INFINITY));
    	System.out.println(fpToString(0));
    	
    	//researching IEEE754 rawint bits
    	System.out.println(fpToString(Float.floatToRawIntBits(0.1f)));
    	System.out.println(fpToString(Float.floatToRawIntBits(6.6f/66)));
    	

    }
}
